import { formatDate } from '../../util/dt';

export interface PaintMessage {
    id: number;
    type: string;
    content: string;
    createTime: string;

    isLoading: boolean;
    image: string;
    imageType: number; // 0原始图片；U后单张图片1~4
    taskId: string;
    imageProgess: number;
    prompt: string;
    status: number;
}

export const createMessage = (content: string, type: string = 'default', isLoading: boolean = false): PaintMessage => {
    return {
        id: new Date().getTime(), type: type, content, createTime: formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss'),
        isLoading, image: '', imageType: 0, taskId: '', imageProgess: 0, prompt: content, status: 0
    };
};

export const createImgMessage = (image: string, prompt: string, type: string = 'robot', isLoading: boolean = true): PaintMessage => {
    return {
        image: image, id: new Date().getTime(), type: type, content: '', createTime: formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss'),
        isLoading, imageType: 0, taskId: '', imageProgess: 0, prompt: prompt, status: 0
    };
};
export const paintDefaultMessage = createMessage(
    "欢迎来到幻景之旅。在下方输入框输入您想象的画面描述词，发送后即可进行绘图，" +
    // "<span style='color: gray; font-size: 12px;'>🧙🏻‍♂️ 英文关键词参考示例：boy,horse,forest等</span>\n" +
    // "<span style='color: orange; font-size: 12px;'>图片生成可能需要1~5分钟⏰，请耐心等待</span>\n")
    "图片生成可能需要1~5分钟⏰，请耐心等待\n")


export enum TaskStatus {
    EXCEPTION01 = -1,
    EXCEPTION02 = -2,
    EXCEPTION03 = -3,
    IDLE = 1000,
    PROCESSIN = 1001,
    FINISH = 1200,
}

export const isTaskException = (status: number) => {
    return status === TaskStatus.EXCEPTION01 || status === TaskStatus.EXCEPTION02 || status === TaskStatus.EXCEPTION03
}

export enum MJApiPath {
    NONE = '',
    SUBMIT = '/mj/submit',
    SUBMIT_CHANGE = '/mj/submit/change',
    TASK_TRACE = '/mj/task/trace',
}

export interface MJRequest {
    url: MJApiPath,
    action?: string;
    prompt?: string;
    taskId?: string;
    index?: number;
    state?: string;
}

export interface MJSubmitResp {
    code: number;
    errMsg: string;
    taskId: string;
    progress: number;
    imageUrl: string;
}

export const createMjGetImageRequest = (prompt: string): MJRequest => {
    return {
        url: MJApiPath.SUBMIT,
        action: "IMAGINE",
        prompt: prompt,
        taskId: "1320098173412546",
        index: 3,
        state: "test:22"
    };
}

export const createMjRequest = (api: MJApiPath, prompt = '', taskId = ''): MJRequest => {
    switch (api) {
        case MJApiPath.SUBMIT:
            return {
                url: MJApiPath.SUBMIT,
                action: "IMAGINE",
                prompt: prompt,
                taskId: "1320098173412546",
                index: 3,
                state: "test:22"
            }
        case MJApiPath.SUBMIT_CHANGE:
            return {
                url: MJApiPath.SUBMIT,
                action: "VARIATION",
                taskId: taskId,
            }
        case MJApiPath.TASK_TRACE:
            return {
                url: MJApiPath.SUBMIT,
                taskId: taskId,
            }
        default:
            return {
                url: MJApiPath.NONE,
            }
    }
}

export const createMjSubmitChangeRequest = (taskId: string, index: number): MJRequest => {
    return {
        url: MJApiPath.SUBMIT_CHANGE,
        action: "VARIATION",
        taskId: taskId,
        index: index,
    }
}